package persistence;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import model.User;

public class ValidationMethods {

    User userValidate;

    public ValidationMethods(User user) {
        userValidate = user;
    }

    private boolean validateName(String userName) {
        String namePattern = "[A-Z]([a-zA-ZñÑáéíóúÁÉÍÓÚ ])*";
        if (userName.matches(namePattern) && checkLength(userName, 0, 30)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean validateNickName(String userNickName) {
        if (checkLength(userNickName, 0, 30)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean validateEmail(String userEmail) {
        String emailPattern =
                "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        Pattern pattern = Pattern.compile(emailPattern);
        Matcher matcher = pattern.matcher(userEmail);
        if (matcher.matches() && checkLength(userEmail, 0, 50)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean validatePassword(String userPassword) {
        if (checkLength(userPassword, 0, 30)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean checkLength(String data, int minLength, int maxLength) {
        int length = data.length();
        boolean isGoodLength = length > minLength && length < maxLength;
        return isGoodLength;
    }

    public boolean validateAllMethods() {
        if (validateName(userValidate.getUserName())
                && validateNickName(userValidate.getUserNickName())
                && validateEmail(userValidate.getUserEmail())
                && validatePassword(userValidate.getUserPassword())) {
            return true;
        } else {
            return false;
        }
    }
}
